import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 保存Cookie
def save_cookies(driver, path):
    with open(path, 'w') as filehandler:
        json.dump(driver.get_cookies(), filehandler)

# 加载Cookie // 15338316965
def load_cookies(driver, path):
    with open(path, 'r') as cookiesfile:
        cookies = json.load(cookiesfile)
    for cookie in cookies:
        driver.add_cookie(cookie)

# 首次登录并保存Cookie
driver = webdriver.Chrome()
driver.get("https://www.eduplus.net/course/course/ac583695451c43779c1bdb042ebc465b/HomeworkPage?userRole=")
# 手动完成登录...
save_cookies(driver, "cookies.json")
driver.quit()

# 下次启动时加载Cookie
driver = webdriver.Chrome()
driver.get("https://www.eduplus.net/course/course/ac583695451c43779c1bdb042ebc465b/HomeworkPage?userRole=")  # 先访问目标网站
load_cookies(driver, "cookies.json")
driver.refresh()  # 刷新页面使Cookie生效